package com.zhuang.service;

import com.zhuang.dto.CommentDTO;
import com.zhuang.enums.CommentTypeEnum;
import com.zhuang.enums.NotificationStatusEnum;
import com.zhuang.enums.NotificationTypeEnum;
import com.zhuang.exception.CustomizeErrorCode;
import com.zhuang.exception.CustomizeException;
import com.zhuang.mapper.*;
import com.zhuang.pojo.Comment;
import com.zhuang.pojo.Notification;
import com.zhuang.pojo.Question;
import org.apache.commons.lang3.StringUtils;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CommentService {

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    QuestionMapper questionMapper;

    @Autowired
    QuestionExtMapper questionExtMapper;

    @Autowired
    CommentExtMapper commentExtMapper;

    @Autowired
    NotificationMapper notificationMapper;

    @Transactional()
    public void insert(Comment comment) {
        if (StringUtils.isBlank(comment.getContent())) {
            throw new CustomizeException(CustomizeErrorCode.COMMENT_CONTENT_ISEMPTY);
        }
        if (comment.getParentId() == null || comment.getParentId() == 0) {
            throw new CustomizeException(CustomizeErrorCode.TARGET_PARAM_NOT_FOUND);
        }
        if (comment.getType() == null || !CommentTypeEnum.isExist(comment.getType())) {
            throw new CustomizeException(CustomizeErrorCode.TYPE_PARAM_WRONG);
        }
        if (comment.getType() == CommentTypeEnum.COMMENT.getCode()) {
            // 回复评论
            Comment parentComment = commentMapper.selectByPrimaryKey(comment.getParentId());
            if (parentComment == null) {
                throw new CustomizeException(CustomizeErrorCode.COMMENT_PARENTID_NOT_FOUND);
            }
            commentMapper.insert(comment);

            // 插入回复
            createNotify(comment, parentComment.getCommentator(), NotificationTypeEnum.REPLY_COMMENT.getType());
        } else {
            // 回复问题
            Question question = questionMapper.selectByPrimaryKey(comment.getParentId().intValue());
            if (question == null) {
                throw new CustomizeException(CustomizeErrorCode.QUESTION_PARENTID_NOT_FOUND);
            }
            commentMapper.insert(comment);
            questionExtMapper.incCommentCount(comment.getParentId().intValue());
            // 插入回复
            createNotify(comment, question.getCreator(), NotificationTypeEnum.REPLY_QUESTION.getType());
        }


    }
    // 插入回复
    private void createNotify(Comment comment, Integer receiver, Integer type) {
        Notification notification = new Notification();
        notification.setGmtCreate(System.currentTimeMillis());
        notification.setNotifier(comment.getCommentator().longValue());
        notification.setReceiver(receiver.longValue());
        notification.setOuterid(comment.getParentId());
        notification.setStatus(NotificationStatusEnum.UNREAD.getStatus());
        notification.setType(type);
        notificationMapper.insert(notification);
    }

    public List<CommentDTO> getByParentId(Integer id, Integer type) {
        List<CommentDTO> commentDTOList = commentExtMapper.getByParentId(id.longValue(), type);
        return commentDTOList;
    }
}
